package com.huacai.orders.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.huacai.common.core.domain.entity.SysUser;
import com.huacai.common.utils.SecurityUtils;
import com.huacai.orders.domain.OrderDetail;
import com.huacai.system.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huacai.common.annotation.Log;
import com.huacai.common.core.controller.BaseController;
import com.huacai.common.core.domain.AjaxResult;
import com.huacai.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.huacai.orders.domain.Orders;
import com.huacai.orders.service.IOrdersService;
import com.huacai.common.utils.poi.ExcelUtil;
import com.huacai.common.core.page.TableDataInfo;

/**
 * 酒店订单Controller
 *
 * @author hsj
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/huacai/orders")
@Slf4j
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询酒店订单列表
     */
//    @PreAuthorize("@ss.hasPermi('huacai:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders)
    {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }

    @GetMapping("/listByUserId")
    public TableDataInfo listByUserId(Orders orders)
    {
        startPage();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<Orders> list = new ArrayList<>();
        if(SecurityUtils.getLoginUser().getUser().isAdmin()){
            list = ordersService.selectOrdersList(orders);
        } else {
            list = ordersService.selectOrdersListByUserId(orders,userId);
        }
        return getDataTable(list);
    }

    @GetMapping("/orderDetail/{orderId}")
    public AjaxResult orderDetail(@PathVariable String orderId)
    {
        System.out.println(orderId);
//        startPage();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<OrderDetail> list = new ArrayList<>();
        if(SecurityUtils.getLoginUser().getUser().isAdmin()){
            list = ordersService.selectOrderDetail(orderId, null);
        } else {
            list = ordersService.selectOrderDetail(orderId,userId);
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出酒店订单列表
     */
//    @PreAuthorize("@ss.hasPermi('huacai:orders:export')")
    @Log(title = "酒店订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "酒店订单数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.importTemplateExcel(response, "酒店订单数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "酒店订单", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('huacai:orders:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        InputStream inputStream = file.getInputStream();
        List<Orders> list = util.importExcel(inputStream );
        inputStream.close();
        int count = ordersService.batchInsertOrders(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取酒店订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('huacai:orders:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(ordersService.selectOrdersByOrderId(orderId));
    }

    /**
     * 新增酒店订单
     */
//    @PreAuthorize("@ss.hasPermi('huacai:orders:add')")
    @Log(title = "酒店订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        Date checkInDate = orders.getCheckInDate();//入住日期
        Date checkOutDate = orders.getCheckOutDate();//退房日期
        Integer between = Math.toIntExact(DateUtil.between(checkInDate, checkOutDate, DateUnit.DAY));//总天数
//        System.out.println(between);
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改酒店订单
     */
//    @PreAuthorize("@ss.hasPermi('huacai:orders:edit')")
    @Log(title = "酒店订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除酒店订单
     */
//    @PreAuthorize("@ss.hasPermi('huacai:orders:remove')")
    @Log(title = "酒店订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(ordersService.deleteOrdersByOrderIds(orderIds));
    }
}
